package com.imooc.town.service.impl;

import com.imooc.town.dto.OrderDTO;
import com.imooc.town.enums.ResultEnum;
import com.imooc.town.exception.SellException;
import com.imooc.town.service.OrderService;
import com.imooc.town.utils.JsonUtil;
import com.imooc.town.utils.MathUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.BestPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * create by song on 2018/3/24
 * To change this template use File | Settings | File Templates.
 */


@Slf4j
@Service
public class PayServiceImpl implements com.imooc.town.service.PayService {

    private static final String ORDER_NAME = "微信订单支付";


    @Autowired
    private BestPayService bestPayService;

    @Autowired
    private OrderService orderService;

    @Override
    public PayResponse cteate(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest ();
        payRequest.setOpenid ( orderDTO.getBuyerOpenid () );
        payRequest.setOrderId ( orderDTO.getOrderId () );
        payRequest.setOrderAmount ( orderDTO.getOrderAmount ().doubleValue () );
        payRequest.setOrderName ( ORDER_NAME );
        payRequest.setPayTypeEnum ( BestPayTypeEnum.WXPAY_H5 );
        log.info ( "【微信支付】发起支付，reuqest={}", JsonUtil.toJson ( payRequest ) );
        PayResponse payResponse = bestPayService.pay ( payRequest );
        log.info ( "【微信支付】发起支付，response={}", JsonUtil.toJson ( payResponse ) );
        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
//        1、验证签名
//        2、验证状态
//        3、校验金额
//        4、支付人
        PayResponse payResponse = bestPayService.asyncNotify ( notifyData );
        OrderDTO orderDTO = orderService.findOne ( payResponse.getOrderId () );
        if (orderDTO == null) {
            log.error ( "【微信支付】异步通知，订单不存在，orderId={}", orderDTO.getOrderId () );
            throw new SellException ( ResultEnum.ORDER_NOT_EXIST );
        }
        if (!MathUtil.equals ( orderDTO.getOrderAmount ().doubleValue (), payResponse.getOrderAmount () )) {
            log.error ( "【微信支付】，订单金额不一致，orderId={}，微信支付金额={}，系统金额={}",
                    payResponse.getOrderId (), payResponse.getOrderAmount (), orderDTO.getOrderAmount () );
            throw new SellException ( ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR );
        }
        orderService.paid ( orderDTO );
        return payResponse;

    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO){
        RefundRequest refundRequest = new RefundRequest ();
        refundRequest.setOrderId ( orderDTO.getOrderId () );
        refundRequest.setOrderAmount ( orderDTO.getOrderAmount ().doubleValue () );
        refundRequest.setPayTypeEnum ( BestPayTypeEnum.WXPAY_H5 );
        log.info ( "【微信退款】request={}",JsonUtil.toJson ( refundRequest ));
        RefundResponse refundResponse = bestPayService.refund ( refundRequest );
        log.info ( "【微信退款】response={}",JsonUtil.toJson ( refundResponse ) );
        return refundResponse;
    }


}
